package yxy.game.pm2.active.command.weeklycard;

import yxy.apple.logger.Logger;
import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_周卡;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyActiveTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Point;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.CurrencyHandle;

/**
 * 周卡_目标任务 兑换货币
 * @author Dell
 *
 */
@MessageType(proto = Command.ActWeeklyCardActiveTaskCurrency, classtype = ActiveMessage.ActWeeklyCardActiveTaskCurrencyRequest.class)
public class CMD_Active_周卡_活跃任务_Currency extends Command<ActiveMessage.ActWeeklyCardActiveTaskCurrencyRequest> {

	private static final Logger logger = Logger.create(CMD_Active_周卡_活跃任务_Currency.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActWeeklyCardActiveTaskCurrencyRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActWeeklyCardActiveTaskCurrencyResponse.Builder response = ActiveMessage.ActWeeklyCardActiveTaskCurrencyResponse.newBuilder();
		
		ActiveModule_周卡 am = ActiveModule_周卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		WeeklyActiveTask wat = am.getWat();
		
		long point36 = Point.get(playercode, CurrencyType.CT_周卡_货币1,false);
		
		Currency cur = Currency.create(CurrencyType.CT_周卡_货币1, -1, point36);
		new CurrencyHandle().cost(playercode, CCReason.活动_周卡, cur);
		
		int keynum = wat.getKeynum();
		int bili = fomula.周卡种树兑换比例.intValue();
		
		keynum = (int) (keynum + point36 * bili);
		wat.setKeynum(keynum);
		am.set();
		
		response.setKeynum(keynum);
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWeeklyCardActiveTaskCurrencyResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
